package com.wgh.product.controller;

import com.wgh.common.vo.Result;
import com.wgh.product.dto.SkuDTO;
import com.wgh.product.entity.shop.Sku;
import com.wgh.product.service.SkuService;
import com.wgh.product.converter.SkuConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * SKU控制器
 * 提供SKU相关的RESTful API
 */
@RestController
@RequestMapping("/api/sku")
public class SkuController {

    @Autowired
    private SkuService skuService;

    @Autowired
    private SkuConverter skuConverter;

    /**
     * 根据商品ID查询SKU列表
     */
    @GetMapping("/product/{productId}")
    public Result<List<SkuDTO>> getSkuListByProductId(@PathVariable Long productId) {
        try {
            List<Sku> skus = skuService.getSkuListByProductId(productId);
            return Result.success(skuConverter.toDTOList(skus));
        } catch (Exception e) {
            return Result.error("根据商品ID查询SKU列表失败：" + e.getMessage());
        }
    }

    /**
     * 根据SKU编号查询SKU
     */
    @GetMapping("/code/{skuCode}")
    public Result<SkuDTO> getSkuByCode(@PathVariable String skuCode) {
        try {
            Sku sku = skuService.getSkuByCode(skuCode);
            if (sku == null) {
                return Result.success(null);
            }
            return Result.success(skuConverter.toDTO(sku));
        } catch (Exception e) {
            return Result.error("根据SKU编号查询SKU失败：" + e.getMessage());
        }
    }

    /**
     * 批量更新SKU库存
     */
    @PutMapping("/stock/batch")
    public Result<Boolean> updateStockBatch(@RequestParam List<Long> skuIds, @RequestParam Integer stock) {
        try {
            boolean result = skuService.updateStockBatch(skuIds, stock);
            return result ? Result.success(true) : Result.error("批量更新SKU库存失败");
        } catch (Exception e) {
            return Result.error("批量更新SKU库存失败：" + e.getMessage());
        }
    }

    /**
     * 批量更新SKU价格
     */
    @PutMapping("/price/batch")
    public Result<Boolean> updatePriceBatch(@RequestParam List<Long> skuIds, @RequestParam Double price) {
        try {
            boolean result = skuService.updatePriceBatch(skuIds, price);
            return result ? Result.success(true) : Result.error("批量更新SKU价格失败");
        } catch (Exception e) {
            return Result.error("批量更新SKU价格失败：" + e.getMessage());
        }
    }

    /**
     * 批量更新SKU状态
     */
    @PutMapping("/status/batch")
    public Result<Boolean> updateStatusBatch(@RequestParam List<Long> skuIds, @RequestParam Integer status) {
        try {
            boolean result = skuService.updateStatusBatch(skuIds, status);
            return result ? Result.success(true) : Result.error("批量更新SKU状态失败");
        } catch (Exception e) {
            return Result.error("批量更新SKU状态失败：" + e.getMessage());
        }
    }

    /**
     * 扣减SKU库存
     */
    @PutMapping("/{id}/deduct-stock")
    public Result<Boolean> deductStock(@PathVariable("id") Long skuId, @RequestParam Integer quantity) {
        try {
            boolean result = skuService.deductStock(skuId, quantity);
            return result ? Result.success(true) : Result.error("扣减SKU库存失败");
        } catch (Exception e) {
            return Result.error("扣减SKU库存失败：" + e.getMessage());
        }
    }

    /**
     * 增加SKU库存
     */
    @PutMapping("/{id}/add-stock")
    public Result<Boolean> addStock(@PathVariable("id") Long skuId, @RequestParam Integer quantity) {
        try {
            boolean result = skuService.addStock(skuId, quantity);
            return result ? Result.success(true) : Result.error("增加SKU库存失败");
        } catch (Exception e) {
            return Result.error("增加SKU库存失败：" + e.getMessage());
        }
    }

    /**
     * 获取SKU详情
     */
    @GetMapping("/{id}")
    public Result<SkuDTO> getSkuDetail(@PathVariable Long id) {
        try {
            Sku sku = skuService.getSkuDetail(id);
            if (sku == null) {
                return Result.success(null);
            }
            return Result.success(skuConverter.toDTO(sku));
        } catch (Exception e) {
            return Result.error("获取SKU详情失败：" + e.getMessage());
        }
    }

    /**
     * 根据商品ID列表查询SKU
     */
    @GetMapping("/product/batch")
    public Result<List<SkuDTO>> getSkuListByProductIds(@RequestParam List<Long> productIds) {
        try {
            List<Sku> skus = skuService.getSkuListByProductIds(productIds);
            return Result.success(skuConverter.toDTOList(skus));
        } catch (Exception e) {
            return Result.error("根据商品ID列表查询SKU失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID查询SKU
     */
    @GetMapping("/get/{id}")
    public Result<SkuDTO> getById(@PathVariable Long id) {
        try {
            Sku sku = skuService.getById(id);
            if (sku == null) {
                return Result.success(null);
            }
            return Result.success(skuConverter.toDTO(sku));
        } catch (Exception e) {
            return Result.error("根据ID查询SKU失败：" + e.getMessage());
        }
    }

    /**
     * 批量查询SKU
     */
    @GetMapping("/batch")
    public Result<List<SkuDTO>> listByIds(@RequestParam List<Long> ids) {
        try {
            List<Sku> skus = skuService.listByIds(ids);
            return Result.success(skuConverter.toDTOList(skus));
        } catch (Exception e) {
            return Result.error("批量查询SKU失败：" + e.getMessage());
        }
    }

    /**
     * 新增SKU
     */
    @PostMapping
    public Result<Boolean> save(@RequestBody SkuDTO skuDTO) {
        try {
            Sku sku = skuConverter.toEntity(skuDTO);
            boolean result = skuService.save(sku);
            return result ? Result.success(true) : Result.error("新增SKU失败");
        } catch (Exception e) {
            return Result.error("新增SKU失败：" + e.getMessage());
        }
    }

    /**
     * 更新SKU
     */
    @PutMapping
    public Result<Boolean> update(@RequestBody SkuDTO skuDTO) {
        try {
            Sku sku = skuConverter.toEntity(skuDTO);
            boolean result = skuService.updateById(sku);
            return result ? Result.success(true) : Result.error("更新SKU失败");
        } catch (Exception e) {
            return Result.error("更新SKU失败：" + e.getMessage());
        }
    }

    /**
     * 删除SKU
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        try {
            boolean result = skuService.removeById(id);
            return result ? Result.success(true) : Result.error("删除SKU失败");
        } catch (Exception e) {
            return Result.error("删除SKU失败：" + e.getMessage());
        }
    }

    /**
     * 批量删除SKU
     */
    @DeleteMapping("/batch")
    public Result<Boolean> deleteBatch(@RequestParam List<Long> ids) {
        try {
            boolean result = skuService.removeByIds(ids);
            return result ? Result.success(true) : Result.error("批量删除SKU失败");
        } catch (Exception e) {
            return Result.error("批量删除SKU失败：" + e.getMessage());
        }
    }
}